package com.jframe.sys.controller.listener;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.jframe.sso.model.CurUser;
import com.jframe.sso.model.LoginLogs;
import com.jframe.sso.model.TotleVisitNum;
import com.jframe.sso.service.LoginLogService;
import com.jframe.sso.service.TotleVisitNumService;
import com.jframe.sys.controller.utils.SessionContext;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.SpringUtil;



public class OnlineSessionListener implements HttpSessionListener,
		ServletContextListener, HttpSessionAttributeListener {
	// 获取application对象
	ServletContext application = null;
	HttpSession session = null;
	Set<String> set = new HashSet<String>(); // 存放在线已注册用户的用户名
	int count, count1, count2 = 0;
	int totleVisitNum=0;
	
	public void sessionCreated(HttpSessionEvent arg0) {
		count = (Integer) application.getAttribute("anonyUserCount");
		count++;
		application.setAttribute("anonyUserCount", count);
		count1 = (Integer) application.getAttribute("anonyUserCount");
		count2 = (Integer) application.getAttribute("registerUserCount");
		totleVisitNum = (Integer) application.getAttribute("systemTotleVisitNum_");
		totleVisitNum++;
		count = count1 + count2;
		application.setAttribute("allUserCount", count);
		application.setAttribute("systemTotleVisitNum_", totleVisitNum);
	}
	
	public void sessionDestroyed(HttpSessionEvent arg0) {
		//退出登录时注册用户减少1，游客用户增加1
		Set<String> set = (Set<String>) application.getAttribute("registerUser");
		CurUser user = (CurUser)arg0.getSession().getAttribute("curUser");
		LoginLogService loginLogService = (LoginLogService)SpringUtil.getBean("loginLogService");
		LoginLogs loginLogs = loginLogService.getLoginLogsByProperties(user.getSessionId());
		loginLogs.setLoginOutTime(CalendarUtil.getDateTime());
		loginLogService.save(loginLogs);
		set.remove(user.getSessionId());
		application.setAttribute("registerUser", set);
		count = (Integer) application.getAttribute("registerUserCount");
		if (count > 0) {
			count--;
		}
		application.setAttribute("registerUserCount", count);
		count1 = (Integer) application.getAttribute("anonyUserCount");//游客
		count1++;
		count = count1 + count;
		application.setAttribute("allUserCount", count);
	}

	public void attributeAdded(HttpSessionBindingEvent arg0) {
		session = arg0.getSession();
		if (session.getAttribute("curUser") != null) {
			CurUser curUser = (CurUser) session.getAttribute("curUser");
			set = (Set<String>) application.getAttribute("registerUser");
			// 新用户登陆
			if (!set.contains(curUser.getSessionId())) {
				SessionContext.AddSession(session);
				set.add(curUser.getSessionId());
				count = (Integer) application.getAttribute("registerUserCount");
				count++;
				application.setAttribute("registerUserCount", count);
				count = (Integer) application.getAttribute("anonyUserCount");
				if (count > 0) {
					count--;
				}
				application.setAttribute("anonyUserCount", count);
			}
		}
		count1 = (Integer) application.getAttribute("anonyUserCount");
		count2 = (Integer) application.getAttribute("registerUserCount");
		count = count1 + count2;
		application.setAttribute("allUserCount", count);
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}
 
	public void contextInitialized(ServletContextEvent arg0) {
		// tomcat启动时创建用于保存人数的application对象
		application = arg0.getServletContext();
		TotleVisitNumService totleVisitNumService = (TotleVisitNumService)SpringUtil.getBean("totleVisitNumService");
		TotleVisitNum totleVisitNum = totleVisitNumService.load(1);
		application.setAttribute("systemTotleVisitNum_", totleVisitNum.getTotleVisitNum());
		application.setAttribute("allUserCount", new Integer(0));// 总在线人数
		application.setAttribute("registerUser", set);
		application.setAttribute("registerUserCount", new Integer(0));// 已注册用户数
		application.setAttribute("anonyUserCount", new Integer(0));// 匿名用户数
	}
}
